﻿using System;
using System.Collections;
using System.Threading;

namespace HttpModuleAccessControl
{
    public class Permissions : CollectionBase, IEnumerable
    {
        static ReaderWriterLock rwlock = new ReaderWriterLock();

        public Permissions()
        {
            //do nothing in this version
        }

        /// <summary>
        /// Load permissions from database
        /// </summary>
        /// <param name="loadPermissionsFromDB"></param>
        public Permissions(bool loadPermissionsFromDB)
        {
            //Log.Write("Permissions class, contructor for loadPermissionsFromDB is entered.");

            Permissions pers = Database.GetPermissions();

            if (List.Count >= 0)
                List.Clear();

            rwlock.AcquireWriterLock(1000);
            foreach (Permission per in pers)
                List.Add(per);

            rwlock.ReleaseWriterLock();

            //Log.Write("Permissions class, contructor for loadPermissionsFromDB is finished.");
        }

 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public void Add(Permission p)
        {
            for (int i = 0; i < List.Count; i++)
            {
                if (((Permission)List[i]).PermissionID == p.PermissionID)
                    return;
            }

            rwlock.AcquireWriterLock(1000);
            List.Add(p);
            rwlock.ReleaseWriterLock();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public void Remove(int index)
        {
            if (index > Count - 1 || index < 0)
            {
                //throw new Exception("Index out of bounds");
                // return without an exception in this version
                return;
            }

            rwlock.AcquireWriterLock(1000);
            List.RemoveAt(index);
            rwlock.ReleaseWriterLock();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public Permission Item(int Index)
        {
            rwlock.AcquireReaderLock(500);
            Permission p = (Permission)List[Index];
            rwlock.ReleaseReaderLock();
            return p;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PermissionID"></param>
        /// <returns></returns>
        public Permission Element(int PermissionID)
        {
            Permission per = new Permission();

            rwlock.AcquireReaderLock(500);
            for (int i = 0; i < List.Count; i++)
            {
                if (((Permission)List[i]).PermissionID == PermissionID)
                {
                    per = (Permission)List[i];
                    break;
                }
            }
            rwlock.ReleaseReaderLock();
            return per;
        }

        /// <summary>
        /// not use in this version.
        /// </summary>
        /// <param name="PermissionID"></param>
        /// <returns></returns>
        //public bool Contains(int PermissionID)
        //{
        //    bool _answer = false;

        //    int _size = List.Count;

        //    for (int i = 0 ; i < _size ; i++)
        //    {
        //        Permission p = (Permission)List[i];
        //        if (p.PermissionID == PermissionID)
        //        {
        //            _answer = true;
        //            break;
        //        }
        //    }

        //    return _answer;
        //}

        /// <summary>
        /// not use in this version.
        /// </summary>
        /// <param name="PermissionName"></param>
        /// <returns></returns>
        //public bool Contains(string PermissionName)
        //{
        //    bool _answer = false;
        //    int _size = List.Count;

        //    for (int i = 0; i < _size; i++)
        //    {
        //        Permission p = (Permission)List[i];
        //        if (p.PermissionName.ToUpper() == PermissionName.ToUpper())
        //        {
        //            _answer = true;
        //            break;
        //        }
        //    }

        //    return _answer;
        //}


    }
}
